package org.amse.korovkin.ciphering;

import org.amse.korovkin.ciphering.model.cipher.monocipher.MonoAttacker;
import org.amse.korovkin.ciphering.model.cipher.monocipher.MonoCipher;
import java.io.*;
import java.util.zip.DataFormatException;
import org.amse.korovkin.ciphering.model.*;
import org.amse.korovkin.ciphering.model.cipher.splinewavelet.Spline1WaveletCipher;
import org.amse.korovkin.ciphering.view.*;

/**
 * @author Michail Korovkin
 */
public class Main {

    /**
     * NUM_LETTERS - maximum amount of chars
     * @see org.amse.korovkin.ciphering.ChangeTable
     * @see org.amse.korovkin.ciphering.TableOfFrequency
     * @see org.amse.korovkin.ciphering.BigramFrequency
     */
    public static final int NUM_LETTERS = 64;
    
    public static void main(String[] args) {
        PossibilityTable.fillAlphabet();
        if (args.length == 0) {
            CipheringJFrame jf = new CipheringJFrame();
        } else {
        if (args[0].equals("e") || args[0].equals("-e")) {
            try {
                //MonoCipher.getInstance().encrypt(args[1], args[2], args[3]);
                Spline1WaveletCipher.getInstance().encrypt(args[1], args[2], args[3]);
                System.out.println("Encrypt successful");
            } catch (FileNotFoundException e) {
                System.out.println(e);
                //e.printStackTrace();
                System.out.println("Can not find file.\n " +
                        e.getMessage());
            } catch (IOException e) {
                System.out.println(e);
                //e.printStackTrace();
                System.out.println("Wrong INPUT for encrypt\n " +
                        "Please write <InputFileName>, <OutputFileName>, <KeyFileName>");
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println(e);
                //e.printStackTrace();
                System.out.println("Wrong KeyFile.\n " +
                        "Please write <InputFileName>, <OutputFileName>, <KeyFileName>");
            }
        } else {
            if (args[0].equals("d") || args[0].equals("-d")) {
                try {
                    //MonoCipher.getInstance().decrypt(args[1], args[2], args[3]);
                    Spline1WaveletCipher.getInstance().decrypt(args[1], args[2], args[3]);
                    System.out.println("Decrypt successful");
                } catch (FileNotFoundException e) {
                System.out.println(e);
                //e.printStackTrace();
                System.out.println("Can not find file.\n " +
                        e.getMessage());
                }  catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("Wrong INPUT for decrypt\n " +
                        "Please write <InputFileName>, <OutputFileName>, <KeyFileName>");
                } catch (ArrayIndexOutOfBoundsException e) {
                    e.printStackTrace();
                    System.out.println("Wrong keyFile. \n " +
                            "Please write <InputFileName>, <OutputFileName>, <KeyFileName>");
                }

            } else {
                if (args[0].equals("a") || args[0].equals("-a")) {
                    try {
                        MonoAttacker.attack(args[1], args[2]);
                        System.out.println("Attack successful");
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println("Wrong INPUT for attack\n " +
                            "Please write <InputFileName>, <OutputFileName>");
                    } catch (ArrayIndexOutOfBoundsException e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage() +
                            "Please write <InputFileName>, <OutputFileName>");
                    } catch (DataFormatException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                } else {
                    if (args[0].equals("s") || args[0].equals("-s")) {
                        try {                           
                            Statistic.createEtalonStatistic(args[1]);
                            System.out.println("Statistic created successful");
                        } catch (IOException e) {
                            e.printStackTrace();
                            System.out.println("Wrong FileName for Statistic\n " +
                            "Please write <StatisticFileName>");
                        } catch (ArrayIndexOutOfBoundsException e2) {
                            e2.printStackTrace();
                            System.out.println("Wrong INPUT for encrypt\n " +
                                    "Please write <InputFileName>, <OutputFileName>");
                        } catch (DataFormatException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("Wrong key. USE -e -d -a -s");
                    }
                }
            }
        }
        }
    }
}
